package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateSkuCateRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusSkuCateRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.SkuCateResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.SkuCateService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 货品类别 前端控制器
 * @author yangdongjie
 * @SkuCateController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "货品类别")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class SkuCateController {

	@Autowired
    private SkuCateService skuCateService;

    private static final String REQUEST_MODEL = "/skucate";
    
    
    /**
	 * 货品类别（下拉框）列表（启用状态）
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "货品类别（下拉框）列表（启用状态）",notes = "货品类别（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<SkuCateResponse> list(
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return skuCateService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	
    /**
     * 查询货品类别列表
     * @param <T>
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询货品类别列表",notes = "查询货品类别列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchSkuCate")
    public PagerInfo<List<SkuCateResponse>> searchSkuCate(
    		SearchSkuCateRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return skuCateService.searchSkuCate(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询货品类别
     * @param locId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询货品类别",notes = "查询货品类别")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getSkuCate")
    public SkuCateResponse getSkuCate(
    		@ApiParam(value = "货品类别ID", required = true) 
    		@RequestParam Integer skuCateId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return skuCateService.getSkuCate(skuCateId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建货品类别
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建货品类别",notes = "创建货品类别")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createSkuCate")
    public SkuCateResponse createSkuCate(
    		@RequestBody @Valid CreateSkuCateRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuCateService.createSkuCate(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改货品类别
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改货品类别",notes = "修改货品类别")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateSkuCate")
    public SkuCateResponse updateSkuCate(
    		@RequestBody @Valid UpdateSkuCateRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuCateService.updateSkuCate(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改货品类别状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改货品类别状态",notes = "修改货品类别状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusSkuCateRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return skuCateService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
